package org.erepublik.company.scanner;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;

import org.erepublik.company.CompanyForSale;
import org.erepublik.company.Configuration;
import org.erepublik.company.ECountry;
import org.erepublik.company.service.RawRegionsService;
import org.json.JSONArray;
import org.json.JSONException;




public class CompanyForSaleScanner {

   
    private ErepublikHttpClient client;
    
    public HashMap<Object, ArrayList<CompanyForSale>> map = new HashMap<Object, ArrayList<CompanyForSale>>();   
    
    RawRegionsService rawRegionService;
    public CompanyForSaleScanner(RawRegionsService rawRegionService) {
        this.rawRegionService = rawRegionService;
        client = new ErepublikHttpClient( Configuration.erepLogin ,  Configuration.erepPass  , rawRegionService);
    }
    
    public void scannCompanies() {
        
        ECountry[] countries = ECountry.values();
//        ECountry[] countries = new ECountry[] { ECountry.PORTUGAL };
        
        for (ECountry eCountry : countries) {
            ArrayList<CompanyForSale> companies = new ArrayList<CompanyForSale>();
            
            try {
                companies = client.getCompaniesForSale(eCountry);
            }  catch(Exception e) {
                System.out.println("blad w skanowaniu kraju "+eCountry+" ponawiam probe...");
                try {
                    client.getCompaniesForSale(eCountry);
                }
                catch(Exception e2) {
                    System.out.println("znowu blad, pomijjam kraj "+eCountry);
                }
            }
            
            map.put(eCountry, companies);
        }
        
        
        
    }
    

    public void saveToJSONFile() {
            try {
                String urlToDirectory = Configuration.databasePath + "/companyForSale/";
                System.out.println(urlToDirectory + new Date().getTime() + "-companyForSale.json");
                File file = new File(urlToDirectory + new Date().getTime() + "-companyForSale.json");
                file.createNewFile();
                BufferedWriter out = null;
    
                out = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
    
                JSONArray json = new JSONArray();
                Collection<ArrayList<CompanyForSale>> countries = map.values();
                for (ArrayList<CompanyForSale> country : countries) {
                    try {
                        for (CompanyForSale companyForSale : country) {
                            json.put(companyForSale.toJSONObject());
                        }
                   
                        
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                out.write(json.toString());
                out.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
     
     public static void main(String[] args) {
         
         CompanyForSaleScanner scanner = null;
        try {
            scanner = new CompanyForSaleScanner(new RawRegionsService());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
         scanner.scannCompanies();
         scanner.saveToJSONFile();
         
         Collection<ArrayList<CompanyForSale>> bla = scanner.map.values();
//         for (ArrayList<CompanyForSale> arrayList : bla) {
//             for (CompanyForSale companyForSale : arrayList) {
//                 System.out.println(companyForSale.getCountry() + " Q"+companyForSale.getQuality() + " " + companyForSale.getProductType() + " " + companyForSale.getPrice() + "g");
//             }
//         }
         

         
     }
     
    
}
